﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace DemoGame1
{
    public class Boss : Enemy
    {
        Playscreen playscreen;
        private int wakeup = 0; //1: da tinh day 0: chua tinh day -1:da tinh day roi, khong tinh day nua
        private int damage = 10;
        private Texture2D[] skillSequence;
        private Texture2D[] wakeupSequence;
        public Ball ball;
        public Boss(ContentManager _content, Vector2 _topleft, Vector2 _size, Playscreen _pl)
        {
            content = _content;
            playscreen = _pl;
            standSequence = CreateTexture("Boss\\stand\\Boss_", 6);
            walkSequence = CreateTexture("Boss\\walk\\Boss_", 6);
            wakeupSequence = CreateTexture("Boss\\wakeup\\Boss_", 12);
            attackSequence = CreateTexture("Boss\\attack\\Boss_", 31);

            skillSequence = CreateTexture("Boss\\skill\\Boss_", 6);

            damagedSequence = CreateTexture("Boss\\damaged\\Boss_", 4);
            deadSequence = CreateTexture("Boss\\dead\\Boss_", 9);
            ball = new Ball(_content, "Boss\\skill\\ball\\Boss_", 8,_topleft, new Vector2(63, 98));
            ball.SetOrigin(new Vector2(302, 356));
            ball.Visible = false;
            InitEnemy(_topleft, _size);
            SetOrigin(new Vector2(290, 282));
            SetTransform(SpriteEffects.FlipHorizontally);

            HP = 200;
            this.SetScale(1.5f);

        }
        public override void Update(GameTime gameTime)
        {
            Player player = playscreen.player;
            if (wakeup == 1 && Sprites[0].iTextures + 1 != Sprites[0].nTextures)
            {
                BoundRect = new Rectangle((int)TopLeft.X, (int)TopLeft.Y, (int)_Size.X, (int)_Size.Y);
                this.SetScale(1.5f);
                base.Update(gameTime);
                if (Sprites[0].iTextures + 1 == Sprites[0].nTextures)
                {
                    Status = STAND;
                    SetFrameSequence(STAND);
                    wakeup = -1;

                }
                return;
            }
            if (dead == 0)
            {
                if (wakeup == 0)
                {

                    if (Status == DAMAGED && !player.ball.Collidewith(this) && player.Status != ATK)
                    {
                        Status = STAND;
                        SetFrameSequence(STAND);
                    }

                      
                    if (Collidewith(player))
                    {
                        if (player.Status == ATK)
                        {
                            Random random=new Random();
                            if (random.Next(4) % 2 == 0)//Có kha nang đánh lại player khi bị player đánh
                            {
                                if (Status != ATK)
                                {
                                    Status = ATK;
                                    SetFrameSequence(ATK);
                                    //thaitujim
                                    myGame.mediaControler.PlaySoundEffect(MediaControler.ENEMY.punch);
                                    //
                                }
                                if (player.TopLeft.X < this.TopLeft.X)
                                {
                                    SetTransform(SpriteEffects.None);
                                }
                                else
                                {
                                    SetTransform(SpriteEffects.FlipHorizontally);
                                }
                                Global.BroadcastEvent(Global.EVENTS.GM_PLAYER_DAMAGED, this, this.damage);
                            }
                            else
                            {
                                HP -= 10;
                                if (Status != DAMAGED)
                                {
                                    Status = DAMAGED;
                                    SetFrameSequence(DAMAGED);
                                    //thaitujim
                                    myGame.mediaControler.PlaySoundEffect(MediaControler.ENEMY.damaged);
                                    //
                                }
                            }
                        }
                        else
                        {
                            if (Status != ATK)
                            {
                                Status = ATK;
                                SetFrameSequence(ATK);
                            }
                            if (player.TopLeft.X < this.TopLeft.X)
                            {
                                SetTransform(SpriteEffects.None);
                            }
                            else
                            {
                                SetTransform(SpriteEffects.FlipHorizontally);
                            }
                            Global.BroadcastEvent(Global.EVENTS.GM_PLAYER_DAMAGED, this, this.damage);
                   

                        }

                    }
                    if (player.ball.Collidewith(this))
                    {
                        HP -= 20;
                        player.ball.Collide = true;
                        if (Status != DAMAGED)
                        {
                            Status = DAMAGED;
                            SetFrameSequence(DAMAGED);
                        }
                    }
                    else
                    {
                        player.ball.Collide = false;
                    }
                    if (!player.ball.Collidewith(this) && !Collidewith(player))
                    {
                        Automove();
                    }

                    if (HP <= 0)
                    {
                        if (Status != DEAD)
                        {
                            Status = DEAD;
                          SetFrameSequence(Character.DEAD);
                        }
                        dead = 1;
                        return;


                    }
                }
                else
                {
                    if (Status != SKILL)
                    {
                        Status = SKILL;
                        SetFrameSequence(SKILL);
                        //thaitujim
                        myGame.mediaControler.PlaySoundEffect(MediaControler.ENEMY.skill);
                        //
                    }
                    ActivateSkill();
                    if (Collidewith(player))
                    {
                        if (player.Status == ATK)
                        {
                            HP -= 13;
                            if (Status != DAMAGED)
                            {
                                Status = DAMAGED;
                                SetFrameSequence(DAMAGED);
                            }
                        }
                        

                    }
                    if (player.ball.Collidewith(this))
                    {
                        HP -= 25;
                        player.ball.Collide = true;
                        if (Status != DAMAGED)
                        {
                            Status = DAMAGED;
                            SetFrameSequence(DAMAGED);
                        }
                    }
                    else
                    {
                        player.ball.Collide = false;
                    }
                    if (HP <= 0)
                    {
                        if (Status != DEAD)
                        {
                            Status = DEAD;
                            SetFrameSequence(Character.DEAD);
                        }
                        dead = 1;
                        return;


                    }
                }
            }
            if (dead > 0)
            {
                if (wakeup == -1)
                {
                    
                    ball.Visible = false;
                }
                dead++;
                if (Sprites[0].iTextures + 1 == Sprites[0].nTextures)
                {
                    bUpdate = false;
                    // Visible = false;
                }
                //thaitujim
                if (dead == 1)
                {
                    if (dead < 10)
                    {
                        myGame.mediaControler.PlaySoundEffect(MediaControler.ENEMY.dead);
                    }
                }
                //
                if (dead == 17)
                {

                    Visible = false;
                     if (wakeup == 0)
                     {
                         bUpdate = true;
                         wakeup = 1;
                         dead = 0;// Tinh lai, ko con dead
                         HP = 800;
                         Visible = true;
                         Status = WAKEUP;
                         SetFrameSequence(WAKEUP);

                     }
                  
                 
                }
            }

            BoundRect = new Rectangle((int)TopLeft.X, (int)TopLeft.Y, (int)_Size.X, (int)_Size.Y);
            this.SetScale(1.5f);
            ball.Ballscale = 2.0f;
            ball.Update(gameTime);
            ball.ChangeBoundPosition(0, -120);
            base.Update(gameTime);
        }

        private void ActivateSkill()
        {
            if (playscreen.player.Status == DAMAGED)
            {
                playscreen.player.Status = STAND;
                playscreen.player.SetFrameSequence(STAND);
            }
            if (ball.Visible == false)
            {
                this.ball.Visible = true;

                ball.TopLeft = new Vector2(playscreen.player.TopLeft.X, -50);
            }
            if (ball.Visible)
            {
                ball.adjustTopleft(ball.TopLeft.X, ball.TopLeft.Y + 20);
                
                if (ball.TopLeft.Y > 700)
                    ball.Visible = false;
            }
            if(ball.Collidewith(playscreen.player))
            {
                Global.BroadcastEvent(Global.EVENTS.GM_PLAYER_DAMAGED, this, 350);
            }
        }

        public override void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            
            ball.Draw(spriteBatch, gameTime);
            base.Draw(spriteBatch, gameTime);
        }

        public override void SetFrameSequence(int type)
        {
            if (type == SKILL)
            {
                Sprites[0].Textures = skillSequence;
            }
            if (type == WAKEUP)
            {
                Sprites[0].Textures = wakeupSequence;
            }
            base.SetFrameSequence(type);
        }
      
    }
}
